<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>邮箱登录</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            background-color: #f5f5f5;
        }

        .login-container {
            background-color: white;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            width: 100%;
            max-width: 400px;
        }

        .form-group {
            margin-bottom: 20px;
        }

        .form-group label {
            display: block;
            margin-bottom: 5px;
            color: #333;
        }

        .form-group input {
            width: 100%;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }

        .code-group {
            display: flex;
            gap: 10px;
        }

        .code-group input {
            flex: 1;
        }

        .code-group button {
            width: 120px;
            white-space: nowrap;
        }

        button {
            background-color: #007bff;
            color: white;
            padding: 10px 15px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            width: 100%;
        }

        button:hover {
            background-color: #0056b3;
        }

        button:disabled {
            background-color: #cccccc;
            cursor: not-allowed;
        }

        .error-message {
            color: #dc3545;
            font-size: 14px;
            margin-top: 5px;
            display: none;
        }
    </style>
</head>
<body>
    <div class="login-container">
        <h2 style="text-align: center; margin-bottom: 30px;">邮箱登录</h2>
        <form id="loginForm" onsubmit="return false;">
            <div class="form-group">
                <label for="email">邮箱地址</label>
                <input type="email" id="email" required placeholder="请输入邮箱地址">
                <div id="emailError" class="error-message"></div>
            </div>
            <div class="form-group">
                <label for="code">验码</label>
                <div class="code-group">
                    <input type="text" id="code" required placeholder="请输入验证码">
                    <button type="button" id="sendCodeBtn">获取验证码</button>
                </div>
                <div id="codeError" class="error-message"></div>
            </div>
            <div class="form-group">
                <button type="submit" id="loginBtn">登录</button>
            </div>
        </form>
    </div>

    <script>
        const sendCodeBtn = document.getElementById('sendCodeBtn');
        const loginBtn = document.getElementById('loginBtn');
        const emailInput = document.getElementById('email');
        const codeInput = document.getElementById('code');
        const emailError = document.getElementById('emailError');
        const codeError = document.getElementById('codeError');

        let countdown = 60;
        let timer = null;

        function startCountdown() {
            sendCodeBtn.disabled = true;
            sendCodeBtn.textContent = `${countdown}秒后重试`;
            timer = setInterval(() => {
                countdown--;
                sendCodeBtn.textContent = `${countdown}秒后重试`;
                if (countdown === 0) {
                    clearInterval(timer);
                    sendCodeBtn.disabled = false;
                    sendCodeBtn.textContent = '获取验证码';
                    countdown = 60;
                }
            }, 1000);
        }

        function validateEmail(email) {
            const re = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
            return re.test(email);
        }

        // 发送验证码
        sendCodeBtn.addEventListener('click', async () => {
            const email = emailInput.value.trim();
            emailError.style.display = 'none';

            if (!email) {
                emailError.textContent = '请输入邮箱地址';
                emailError.style.display = 'block';
                return;
            }

            if (!validateEmail(email)) {
                emailError.textContent = '请输入有效的邮箱地址';
                emailError.style.display = 'block';
                return;
            }

            try {
                const response = await fetch('/api/email/send-code', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({ email })
                });

                const data = await response.json();
                
                if (data.success) {
                    startCountdown();
                } else {
                    emailError.textContent = data.message;
                    emailError.style.display = 'block';
                }
            } catch (error) {
                emailError.textContent = '发送验证码失败，请稍后重试';
                emailError.style.display = 'block';
            }
        });

        // 验证登录
        loginBtn.addEventListener('click', async () => {
            const email = emailInput.value.trim();
            const code = codeInput.value.trim();
            emailError.style.display = 'none';
            codeError.style.display = 'none';

            if (!email || !validateEmail(email)) {
                emailError.textContent = '请输入有效的邮箱地址';
                emailError.style.display = 'block';
                return;
            }

            if (!code) {
                codeError.textContent = '请输入验证码';
                codeError.style.display = 'block';
                return;
            }

            try {
                const response = await fetch('/api/email/verify-code', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({ email, code })
                });

                const data = await response.json();

                if (data.success) {
                    // 显示成功消息
                    const successMessage = document.createElement('div');
                    successMessage.style.cssText = `
                        position: fixed;
                        top: 50%;
                        left: 50%;
                        transform: translate(-50%, -50%);
                        background-color: rgba(0, 0, 0, 0.8);
                        color: white;
                        padding: 15px 30px;
                        border-radius: 4px;
                        z-index: 1000;
                    `;
                    successMessage.textContent = '登录成功，正在跳转...';
                    document.body.appendChild(successMessage);

                    // 1秒后跳转
                    setTimeout(() => {
                        window.location.href = '/index.html';
                    }, 1000);
                } else {
                    codeError.textContent = data.message;
                    codeError.style.display = 'block';
                }
            } catch (error) {
                codeError.textContent = '验证失败，请稍后重试';
                codeError.style.display = 'block';
            }
        });
    </script>
</body>
</html> 